import {abstractValidator} from "../validator/abstractValidator";
import {mc_validatorResult} from "../model/mc_validatorResult";
/**
 * Created by ybjaa on 2017/7/3.
 */
export function getValidators_v1(){


    return function (target:any,propertyName:string,properyDescriptor:PropertyDescriptor) {

        let oldvalue=properyDescriptor.value;


        properyDescriptor.value=function () {

            let map:Map<string,abstractValidator[]>=target[Symbol.for("validator")];

            if(!map){
                return oldvalue.call(this,true,null);
            }

            let validators=new Array<mc_validatorResult>();

            map.forEach((v,k)=>{
                if(Reflect.has(this,k))
                    validators.push.apply(validators,v.map(v=>v.validator(this[k],this)));
            });

            let falseValidators= validators.filter((v=>!v.result));

            if(falseValidators.length>0){
                return oldvalue.call(this,false,falseValidators);
            }else {
                return oldvalue.call(this,true,null);
            }
        }


        return properyDescriptor;





    }
    

}